package com.base.cn.platform.os.config;

import com.base.cn.platform.os.common.interceptor.BaseDataInterceptor;
import com.base.cn.platform.os.common.interceptor.keyword.KeyWordFilter;
import com.base.cn.platform.os.common.xss.XssCodeFilter;
import com.base.cn.platform.os.interceptor.MobileAccessJumpInterceptor;
import com.base.cn.platform.os.interceptor.MobileUserLoginInterceptor;
import com.base.cn.platform.os.interceptor.RequestTokenInterceptor;
import com.base.cn.platform.os.service.manage.op.keyword.OpKeywordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/**
 * H5配置
 *
 * @author s.li
 * @create 2018-05-11-18:08
 */
@Configuration
public class MobileConfig implements WebMvcConfigurer {

    @Autowired
    private OpKeywordService opKeywordService;
    @Autowired
    private RequestTokenInterceptor requestTokenInterceptor;

    //敏感词、非字过滤路径
    private static List<String> urlList = new ArrayList<>();
    {
        urlList.add("/mobile/uc/class/topic/ajax/save/reply");//话题评论接口
        urlList.add("/mobile/uc/class/topic/ajax/save/report");//举报话题接口
        urlList.add("/mobile/uc/class/topic/ajax/publish");//发表话题
        urlList.add("/mobile/uc/userInfo/updateUserInfo");//修改个人信息
        urlList.add("/mobile/uc/op/feedback/saveUpdateFeedBack");//用户反馈
        urlList.add("/mobile/uc/qa/saveQuestion");//保存问答
        urlList.add("/mobile/uc/qa/reply/saveQaReply");//保存问答回复
        urlList.add("/mobile/uc/qa/add/saveQuestionAdd");//保存问答追问
        urlList.add("/mobile/uc/exam/saveQuestionRecord");//保存试题作答记录
    }
    /**
     * 访问路径拦截器,如果使用非手机浏览器访问时自动路径PC站
     * @return MobileAccessJumpInterceptor
     */
    @Bean
    public MobileAccessJumpInterceptor mobileAccessJumpInterceptor(){
        return new MobileAccessJumpInterceptor();
    }

    /**
     * 内容过滤器
     * @return
     */
    @Bean
    public BaseDataInterceptor baseDataInterceptor(){
        return new BaseDataInterceptor();
    }

    /**
     * 用户登录拦截器
     * @return
     */
    @Bean
    public MobileUserLoginInterceptor mobileUserLoginInterceptor(){
        return new MobileUserLoginInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //registry.addInterceptor(requestTokenInterceptor).addPathPatterns("/mobile/**").excludePathPatterns("/mobile/mobileError");
        //访问重定向拦截排除支付回调路径/mobile/pay/paySuccessCallback/**
        //如果不排除，因为在微信支付成功后的回调是从微信服务器发出的，不是在H5浏览上访问，
        // 所以会重新跳转到web页面，这样会倒致H5微信支付回调失败
        registry.addInterceptor(mobileAccessJumpInterceptor()).addPathPatterns("/mobile/**").excludePathPatterns("/mobile/pay/paySuccessCallback/**");
        registry.addInterceptor(baseDataInterceptor()).addPathPatterns("/mobile/**");
        registry.addInterceptor(mobileUserLoginInterceptor()).addPathPatterns("/mobile/uc/**");
    }

    @Bean
    public FilterRegistrationBean xssCodeFilter(){
        FilterRegistrationBean xssCodeFilter  = new FilterRegistrationBean();
        xssCodeFilter.setFilter(new XssCodeFilter());
        xssCodeFilter.setOrder(2);
        xssCodeFilter.setName("xssCodeFilter");
        xssCodeFilter.setUrlPatterns(urlList);
        return xssCodeFilter;
    }

    /**
     * 敏感词过滤
     * @return FilterRegistrationBean
     */
    @Bean
    public FilterRegistrationBean keyWordFilter(){
        FilterRegistrationBean keyWordFilter = new FilterRegistrationBean();
        keyWordFilter.setFilter(new KeyWordFilter(opKeywordService));
        keyWordFilter.setName("keyWordFilter");
        keyWordFilter.setOrder(1);
        keyWordFilter.setUrlPatterns(urlList);
        return keyWordFilter;
    }
}
